﻿function getParams() {

    var params = {};

    var companyName = $("input[name='companyName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(companyName)) {
        params.companyName = companyName;
    }

    var bankName = $("input[name='bankName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(bankName)) {
        params.bankName = bankName;
    }

    var cardNumber = $("input[name='cardNumber']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(cardNumber)) {
        params.cardNumber = cardNumber;
    }

    var cardName = $("input[name='cardName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(cardName)) {
        params.cardName = cardName;
    }

    var enableStatus = $("select[name='enableStatus']").find("option:selected").val();
    if (!FlashPay.Util.isNullOrEmptySpance(enableStatus)) {
        params.enableStatus = enableStatus;
    }

    var lastUpdateDateSortType = $("select[name='lastUpdateDateSortType']").find("option:selected").val();
    if (!FlashPay.Util.isNullOrEmptySpance(lastUpdateDateSortType)) {
        params.lastUpdateDateSortType = lastUpdateDateSortType;
    }

    return params;
}

function doSearch() {

    var columns = [[
        {
            field: 'companyName',
            title: '公司名称',
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                return val;
            }
        },
        { field: 'bankName', title: '银行名称', width: 100, align: 'left' },
        {
            field: 'cardNumber',
            title: '银行卡号',
            width: 120,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.SubCardNumber(val);
            }
        },
        { field: 'cardName', title: '用户名', width: 100, align: 'left' },
        { field: 'paymentTypeName', title: '付款类型', width: 80, align: 'left' },
        {
            field: 'usingStatus',
            title: '使用状态',
            width: 65,
            align: 'center',
            formatter: function (val, row) {
                var result = "";
                if (val == 1) {
                    result = '<span class="green bold">有效</span>';
                } else {
                    result = '<span class="red bold">冻结</span>';
                }
                return result;
            }
        },
        {
            field: 'enableStatus',
            title: '启用状态',
            width: 65,
            align: 'center',
            formatter: function (val, row) {
                var result = "";
                if (val == 1) {
                    result = '<span class="green bold">启用</span>';
                } else if (val == 2) {
                    result = '<span class="red bold">禁用</span>';
                } else {
                    result = '<span class="red bold">删除</span>';
                }
                return result;
            }
        },
        {
            field: 'balance',
            title: '参考余额',
            width: 100,
            align: 'right'
        },
        { field: 'paymentInterval', title: '付款区间起迄', width: 120, align: 'right' },
        {
            field: 'balanceLastUpdate',
            title: '更新时间',
            width: 120,
            align: 'left',
            styler: function (value, item, index) {
                if (item.timeDifference >= 300 && item.enableStatus == 1) {
                    return 'background-color:red;color:white;';
                }
            }
        },
        { field: 'transportRate', title: '中转费率(%)', width: 100, align: 'left' },
        { field: 'vmClientId', title: '虚拟机编码', width: 100, align: 'left' },
        { field: 'remark', title: '备注', width: 100, align: 'left' },
        {
            field: 'bcId', title: '操作', width: 150, align: 'left',
            formatter: function (val, item) {

                var result = "";

                if (item.enableStatus == 1) {
                    result = '<a class="btn btn-warning btn-xs" data-confirm="{0} <br>确定要<span class=orange>禁用</span>该银行卡？" data-href="/Transfer/TransferCardUpdateStatus?id={1}&status=2">禁用</a>'.format(item.cardName, val);
                } else {
                    result += '<a class="btn btn-success btn-xs" data-confirm="{0} <br>确定要<span class=orange>启用</span>该银行卡？" data-href="/Transfer/TransferCardUpdateStatus?id={1}&status=1">启用</a>'.format(item.cardName, val);
                    result += ' <button type="button" class="btn btn-default btn-xs" data-modal="/view/transfer/transfercardEdit.html?id={0}" data-title="编辑 - {1}-{2}-{3}" data-width="452" data-height="120"><span class="glyphicon glyphicon-edit"></span> 编辑</button>'.format(val, item.bankName, item.cardName, FlashPay.Util.SubCardNumber(item.cardNumber));
                }
                return result;
            }
        }
    ]];

    $("#transfercard").datagrid({
        fit: false,
        loadMsg: '数据加载中，请稍后……',
        url: '/Transfer/TransferCard',
        queryParams: getParams(),
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        pageSize: pageSize,
        pageList: [15, 30, 50, 100, 1000],
        height: $(window).height() - 70,
        idField: 'bcId',
        onLoadSuccess: function (data) {
            pageSize = $('#transfercard').datagrid('options').pageSize;
        },
        fitColumns: true,
        showFooter:true,
        columns: columns
    });
}